package main

import (
	"fmt"
	"sync"
)

/**
 * @description 
 * @time 2018/5/12 19:18
 * @version 
 */
func main() {
	channelDemo()
}

type worker struct {
	in chan int
	done func()
}

func channelDemo() {
	var workers [10] worker
	var wg sync.WaitGroup
	wg.Add(20)
	for i := 0; i < 10; i++ {
		workers[i] = createWorker(i, &wg)
	}

	for i, worker := range workers {
		worker.in <- 'a' + i
	}

	for i, worker := range workers {
		worker.in <- 'A' + i
	}
	wg.Wait()

}

func doWorker(id int, worker worker) {
	for n := range worker.in {
		fmt.Printf("worker %d received %c\n", id, n)
		//通知外面打印结束
		worker.done()
	}
}

func createWorker(id int, wg *sync.WaitGroup) worker {
	w := worker{
		in: make(chan int),
		done: func() {
			wg.Done()
		},
	}
	go doWorker(id,w)
	return w
}
